﻿define(['config'], function (config)
{
    var defaultDependencies = [];//["controllers/menuController"];
    var app = angular.module("dashboard", ["ui.bootstrap", 'ngRoute', 'ngResource', 'ngCookies', 'common', 'localization']);
    var baseUrl = "/Dashboard";
    var baseCodeUrl = "../Modules/NineSeas.Dashboard/";
    var baseTemplateUrl = baseCodeUrl + "Views/Template/";
    
    app.config(
        [
            '$routeProvider',
            '$locationProvider',
            '$controllerProvider',
            '$compileProvider',
            '$filterProvider',
            '$httpProvider',
            '$provide',
            '$injector',

            function ($routeProvider, $locationProvider, $controllerProvider, $compileProvider, $filterProvider, $httpProvider, $provide, $injector) {
                app.lazy =
                {
                    controller: $controllerProvider.register,
                    directive: $compileProvider.directive,
                    filter: $filterProvider.register,
                    factory: $provide.factory,
                    service: $provide.service,
                    provide: $provide,
                    $httpProvider: $httpProvider
                };

                $locationProvider.html5Mode(true);
                
                //var baseScriptUrl = baseCodeUrl + "Scripts/";

                var registerRoute = function (route, path) {
                    $routeProvider.when(baseUrl + path, {
                        templateUrl: baseTemplateUrl + route.templateUrl,
                        caseInsensitiveMatch: true,
                        resolve: {
                            resolver: ['$q', '$rootScope', function ($q, $rootScope) {
                                var deferred = $q.defer();

                                if (route.title) {
                                    $rootScope.title = route.title;
                                }

                                // fix dependencies
                                var mergedDependencies = $.merge($.merge([], defaultDependencies), route.dependencies);
                                var dependencies = [];
                                angular.forEach(mergedDependencies, function (item) {
                                    if (item && item[0] != '/') {
                                        item = item + ".min";
                                    }

                                    dependencies.push(item);
                                });

                                require(dependencies, function () {
                                    $rootScope.$apply(function () {
                                        deferred.resolve();
                                    });
                                });

                                return deferred.promise;
                            }]
                        }
                    });
                };
                if (config.routes !== undefined) {
                    angular.forEach(config.routes, function (route, path) {
                        registerRoute(route, path);
                    });
                }

                if (config.defaultRoutePaths !== undefined) {
                    $routeProvider.otherwise({ redirectTo: baseUrl + config.defaultRoutePaths });
                }

                app.lazy.registerRoute = registerRoute;

                //var appEvents = $injector.get("appEvents");

                //appEvents.triggerInit();
            }
        ])
    .factory("global", function () {
        return {
            baseUrl: baseUrl,
            baseCodeUrl: baseCodeUrl,
            baseTemplateUrl: baseTemplateUrl
        };
    });

    return app;
});